package com.audaque.springboot.foshanupload.core.util;


import java.security.SecureRandom;
import java.util.Random;

public class RandomUtils {
	private static final SecureRandom RANDOM = new SecureRandom();
	public static final String HASH_TAG = "#";
	private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/**
	 * 随机数
	 * @return
	 */
	public static String generateNonceStr() {
		char[] nonceChars = new char[32];
		for (int index = 0; index < nonceChars.length; ++index) {
			nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
		}
		return new String(nonceChars);
	}

	public static String generateNonceTradeno() {
		return "rt" + System.currentTimeMillis() + RandomUtils.getFixLengthInt(8);
	}

	public static String generateExternalAccountPrefix() {
		return RandomUtils.getFixLengthInt(8) + HASH_TAG;
	}
	
	public static String generateNonceTradeno(int index) {
		return "rt" + System.currentTimeMillis() + RandomUtils.getFixLengthInt(3) + String.format("%02d", index);
	}
	
	public static String getFixLenthString(int length) {
		String random = "";
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			random += Integer.toString(r.nextInt(36), 36);
		}
		return random;
	}

	public static String getFixLengthInt(int length) {
		String randomInt = "";
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			randomInt += r.nextInt(10);
		}
		return randomInt;
	}
}
